package cn.bonoon.kernel.descriptors.impl;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Embedded;

import cn.bonoon.kernel.descriptors.EmbeddedPropertyAccessor;
import cn.bonoon.kernel.descriptors.EntityPropertyAccessor;
import cn.bonoon.kernel.descriptors.impl.EntityDescriptorImpl.DescriptorInternalVisitor;

public class EmbeddedPropertyAccessorImpl extends AbstractEntityPropertyAccessor implements EmbeddedPropertyAccessor{
	private final Map<String, EntityPropertyAccessor> properties;
	public EmbeddedPropertyAccessorImpl(DescriptorInternalVisitor visitor, Class<?> cls, 
			String mappedName, Field field, EmbeddedPropertyAccessor embeddedProperty) {
		super(cls, mappedName, field, embeddedProperty);
		this.properties = new HashMap<String, EntityPropertyAccessor>();
		//这里应该对field所定义的类型进行处理
		Class<?> next = type;
		EntityPropertyAccessor epa;
		do {
			for (Field tf : next.getDeclaredFields()) {
				String tn = mappedName + '.' + tf.getName();
				if(tf.isAnnotationPresent(Embedded.class)){
					epa = new EmbeddedPropertyAccessorImpl(visitor, type, tn, tf, this);
				} else {
					epa = new EntityPropertyAccessorImpl(type, tn, tf, this);
				}
				visitor.put(tn, epa);
				properties.put(tf.getName(), epa);
			}
			next = next.getSuperclass();
		} while (null != next && !Object.class.equals(next));
	}
	
	public EmbeddedPropertyAccessorImpl(DescriptorInternalVisitor visitor, Class<?> cls, String mappedName, Field field){
		this(visitor, cls, mappedName, field, null);
	}
}
